(define cyl-current-func-flag #f)
(def/err (gen-sym-list l)
  (let ((end-sym-list '())
	)
    (set! cyl-run-status 'top-level)
    (set! cyl-current-define-function-name "#top-level")
    ;;(y-or-n "gen-sym-list:l=~a\n" l)
    (cond
     ((null? l) end-sym-list)
     ((atom? l)
      (list 'err 0 'gen-sym-list (format "错误提示：在最后生成符号列表的过程中，不应该出现元素[~a]。" l)))
     ((pair? l)
      (if (not (integer? (nth 1 l)))
	  (begin
	  (tr-list (i e l 0)
		   (let ((name (nth 0 e))
			 (typ (nth 3 e))
			 (f-name (nth 5 e))
			 )
		     ;;(y-or-n "gen-sym-list:name-length=~a,typ=~a\n" (if (pair? name) (length name) 0) typ)
		     (cond
		      ((and (pair? name)(eq? typ '函数定义))
		       (set! end-sym-list (append end-sym-list (list (run/err gen-func-list name))))
		       (set! cyl-current-func-flag #f)
		       (set! cyl-run-status 'top-level)
		       (set! cyl-current-define-function-name "#top-level")
		       )
		      ((and (atom? name)(deli-is typ))
		       )
		      ((and (pair? name)(eq? cyl-run-status 'top-level)(com-is typ))
		       ;;(set! end-sym-list (append end-sym-list (list (list cyl-current-define-function-name (run/err gen-com-list name)))))
		       )
		      (else
		       (exit-gen-sym-list (list 'err 0 'gen-sym-list (format "错误提示：在第一级列表解析中，不支持的cyl类型[~a]。" typ)))
		       ))))
	  end-sym-list)
	  ))
     (else
      (exit-gen-sym-list (list 'err 0 'gen-sym-list (format "错误提示：在第一级列表解析中，不支持的列表类型[~a]。" l)))
      )
     )))
(define (deli-is typ)
  (cond
   ((eq? typ '分隔符) #t)
   ((eq? typ '结束分隔符) #t)
   ((eq? typ '语句终结分隔符) #t)
   ((eq? typ '函数定义分隔符) #t)
   (else #f)))
(define (com-is typ)
  (cond
   ((eq? typ '并列语句) #t)
   ((eq? typ '系统宏调用) #t)
   ((eq? typ '系统关键字调用) #t)
   ((eq? typ 'y表达式调用) #t)
   ((eq? typ '无限参数函数调用) #t)
   ((eq? typ 'lisp-call) #t)
   (else #f)))
(define (search-com-list n t com-list)
  (let ((res #f))
     (tr-list (k ek com-list 0)
	      (let ((cur-name (nth 0 ek))
		    (cur-typ (nth 1 ek))
		    )
		;;(y-or-n "search-com-list:name=~a,typ=~a\nn=~a,t=~a\n" cur-name cur-typ n t)
		(if (and (string? cur-name)(string=? n cur-name))
		    (set! res #t))))
     res))
(def/err (push-com n t com-list)
  (if (not (search-com-list n t com-list))
       (if (and (string? n)(string=? n cyl-current-define-function-name))
	   (begin
	     
	     (let ((dict-tl (find-current-coding-list n)))
	       (if dict-tl
		   (let ((f-typ (nth 0 dict-tl)))
		     ;;(y-or-n "dict-tl=~a\nf-typ=~a\n" dict-tl f-typ)
		     (if (or (eq? f-typ '无参局部函数)(eq? f-typ '局部函数)(eq? f-typ '全局局部函数))
			 (exit-push-com (list 'err 0 'push-com (format "错误提示：[~a][~a]不允许自递归定义。严重错误。" n f-typ)))))))
	     (set! cyl-current-func-flag '自递归函数)
	     com-list
	     )
	   (begin
	    
	     (let ((dict-tl (find-current-coding-list n)))
	       ;;(if (string=? n "下一字符") (y-or-n "dict-tl=~a\ndict=~a\n" dict-tl cyl-local-symbol-dict-list))
	       (if dict-tl
		   (set! com-list (append com-list (list (list n (car dict-tl)))))
		   (set! com-list (append com-list (list (list n t))))
		   ))
	     com-list
	     )
	   )
       com-list
       ))
(def/err (push-com-list l com-list)
  (if (null? l)
      com-list
      (if (pair? l)
       (begin
	 (tr-list (li el l 0)
		  (let ((l-name (nth 0 el))
			(l-typ (nth 1 el))
			)
		    (set! com-list (run/err push-com l-name l-typ com-list))
		    ))
	 com-list
	 ))))
(def/err (gen-com-list l)
  (let ((com-list '())
	)
    (if (pair? l)
	(let ((n (nth 0 l))
	      (typ (nth 3 l))
	      (fn (nth 5 l))
	      )
	  ;;(y-or-n "gen-com-list:typ=~a\nn=~a\nfn=~a\n" typ n fn)
	  (cond
	  
	   ((null? n)
	    ;;(list 'err 0 'gen-com-list (format "错误提示：不允许有空列表出现在首项位置。开发错误。"))
	    )
	   ((and (yexp-sym-is typ)(string? fn)(pair? n))
	    ;;(set! com-list (append com-list (list (list fn typ))))
	    (set! com-list (run/err push-com fn typ com-list))
	    ;;(set! com-list (append com-list (gen-com-list n)))
	    (set! com-list (run/err push-com-list (run/err gen-com-list n) com-list))
	    )
	   ((and (string? n)(no-sym-is typ))
	    )
	  
	   ((and (string? n)(func-sym-is typ))
	    ;;(set! com-list (append com-list (list (list n typ))))
	    (set! com-list (run/err push-com n typ com-list))
	    ;;(y-or-n "com-list=~a\n" com-list)
	    )
	   ((and (string? n)(var-sym-is typ))
	    ;;(set! com-list (append com-list (list (list n typ))))
	    (set! com-list (run/err push-com n typ com-list))
	    )
	   ((and (pair? n)(integer? (nth 1 l))(com-is typ))
	    ;;(set! com-list (append com-list (gen-com-list n)))
	    (set! com-list (run/err push-com-list (run/err gen-com-list n) com-list))
	    )
	   ((and (pair? n)(pair? (nth 1 l)))
	    ;;(set! com-list (append com-list (gen-com-list n)(gen-com-list (cdr l))))
	    (tr-list (i e l 0)
		     ;;(y-or-n "i=~a,e=~a\n" i e)
		     ;;(set! com-list (append com-list (gen-com-list e)))
		     (set! com-list (run/err push-com-list (run/err gen-com-list e) com-list))
		     ;;(y-or-n "com-list=~a\n" com-list)
		     )
	    )
	   (else
	    (set! com-list (list 'err 0 'gen-com-list (format "错误提示：[~a]类型未定义或者首项非字符串。开发错误。" typ)))
	   ))
	  ;;(y-or-n "com-list=~a\nn=~a,fn=~a,typ=~a\n" com-list n fn typ)
	  com-list
	  )
	(list 'err 0 'gen-com-list (format "错误提示：不允许调用非列表子项。开发错误。")))))
(def/err (gen-func-list l)
  ;;(y-or-n "gen-func-list:l=~a\nlen=~a\n" l (length l))
   (let ((func-list '())
	)
    (if (= (length l) 3)
	(let ((f1 (nth 0 l))
	      (f2 (nth 1 l))
	      (f3 (nth 2 l))
	      (f-typ #f)
	      )
	  (let ((n (nth 0 f1))
		(typ (nth 3 f1))
		(fn (nth 5 f1))
		)
	    (if (and (string? n)(func-def-is typ))
		(set! cyl-current-define-function-name n)
		(if (and (pair? n)(yexp-func-def-is typ))
		    (set! cyl-current-define-function-name fn)
		    (exit-gen-func-list (list 'err 0 'gen-func-list (format "错误提示：函数名不合规范。开发错误。~a" f1)))))
	    (cyl-fn-coding-get cyl-current-define-function-name)
	    (set! f-typ (find-current-coding-list cyl-current-define-function-name))
	    (if f-typ (set! f-typ (car f-typ) )(exit-gen-func-list (list 'err 0 'gen-func-list (format "错误提示：函数名未定义不合法。开发错误。~a" cyl-current-define-function-name))))
	    )
	  (let ((n (nth 0 f2))
		(typ (nth 3 f2))
		)
	    (if (not (and (string? n)(eq? typ '函数定义分隔符)))
		(exit-gen-func-list (list 'err 0 'gen-func-list (format "错误提示：函数分隔符不合规范。开发错误。")))))
	  (let ((n (nth 0 f3))
		(typ (nth 3 f3))
		(fn (nth 5 f3))
		)
	    (if (and (pair? n)(eq? typ '函数体定义))
		(let ((com-l (run/err gen-com-list n)))
		  (set! func-list (list cyl-current-define-function-name com-l cyl-current-func-flag f-typ))
		  
		  func-list
		  )
		(list 'err 0 'gen-func-list (format "错误提示：函数体不合规范。开发错误。"))))
	  )
	(list 'err 0 'gen-func-list (format "错误提示：函数列表不合规范。开发错误。")))
    ))
(define (yexp-func-def-is typ)
  (cond
   ((eq? typ 'y表达式函数名定义) #t)
   ((eq? typ 'at表达式函数名定义) #t)
   ((eq? typ '无限参数函数名定义) #t)
   (else #f)))
(define (func-def-is typ)
  (cond
   ((eq? typ '无参函数名定义) #t)
   ((eq? typ 'at表达式无参函数名定义) #t)
   (else #f)))
(define (func-sym-is typ)
  (cond
   ((eq? typ '无参函数) #t)
   ((eq? typ '无参局部函数) #t)
   ((eq? typ '无参别名函数) #t)
   ((eq? typ 'lisp系统函数) #t)
   ((eq? typ '系统无参函数) #t)
   (else #f)))
(define (yexp-sym-is typ)
  (cond
   ((eq? typ 'y表达式调用) #t)
   ((eq? typ 'y表达式函数调用) #t)
   ((eq? typ 'y表达式局部函数调用) #t)
   ((eq? typ '无限参数函数调用) #t)
   ((eq? typ '系统函数) #t)
   (else #f)))
(define (var-sym-is typ)
  (cond
   ((eq? typ '全局变量) #t)
   ((eq? typ '局部变量) #t)
   ((eq? typ '外部变量) #t)
   ((eq? typ '参数变量) #t)
   ((eq? typ '参数) #t)
   ((eq? typ '宏引导变量) #t)
   ((eq? typ '全局别名变量) #t)
   ((eq? typ '局部别名变量) #t)
   (else #f)))

(define (no-sym-is typ)
  (cond
   ((cons-is typ) #t)
   ((deli-is typ) #t)
   ((eq? typ '函数名片段) #t)
   ((eq? typ '系统宏) #t)
   ((eq? typ '系统关键字) #t)
   ((eq? typ 'lisp-quote) #t)
   ((eq? typ 'lisp-backquote) #t)
   ((eq? typ 'lisp-unquote) #t)
   ((eq? typ 'lisp-begin) #t)
   ((eq? typ 'lisp-end) #t)
   (else #f)))

		
